import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'monitorpublicopinionM',
  state: {
    id: '', //触发props变化
    eventLabelList: [], //情感标签列表
    oEventLabelList: [], //原有的情感标签列表
    noticeTypeList: [], //公告类型列表
    oNoticeTypeList: [], //原始公告类型列表
    comcodeList: [], //主体名称列表
    noticeTableColumns: undefined, //公告列表表头
    noticeTableData: undefined, //公告列表数据
    noticeTotal: 0, //公告列表总条数
    newsTableColumns: undefined, //新闻列表表头
    newsTableData: undefined, //新闻列表数据
    newsTotal: 0, //新闻列表总条数
    newsInfo: {}, //新闻详情
    newsHoldColumns: undefined, //news相关持仓表头
    newsHoldData: undefined, //news相关持仓数据
    noticeHoldColumns: undefined, //notice相关持仓表头
    noticeHoldData: undefined, //notice相关持仓数据
  },
  effects: {
    //查询主体名称
    *queryComcode({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.queryComcode, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              comcodeList: response.data.slice(0, 20) || [],
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              comcodeList: [],
            },
          });
          message.error(response.msg || '查询主体名称下拉列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            comcodeList: [],
          },
        });
        message.error('查询主体名称下拉列表失败!');
      }
    },
    //查询事件标签下拉列表
    *comNewsEventTag({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.queryComNewsEventTag, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              eventLabelList: response.data || [],
              oEventLabelList: response.data || [],
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              eventLabelList: [],
              oEventLabelList: [],
            },
          });
          message.error(response.msg || '查询事件标签下拉列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            eventLabelList: [],
            oEventLabelList: [],
          },
        });
        message.error('查询事件标签下拉列表失败!');
      }
    },
    //查询公告类型下拉列表
    *comAnnouncementType({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comAnnouncementType, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              noticeTypeList: response.data || [],
              oNoticeTypeList: response.data || [],
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              noticeTypeList: [],
              oNoticeTypeList: [],
            },
          });
          message.error(response.msg || '查询公告类型下拉列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            noticeTypeList: [],
            oNoticeTypeList: [],
          },
        });
        message.error('查询公告类型下拉列表失败!');
      }
    },
    //查询公告列表
    *comAnnouncement({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comAnnouncement, payload);
      if (response) {
        if (response.code == 0) {
          response.data.dataSource.map((item, index) => {
            item.key = index;
          });
          yield put({
            type: 'setState',
            payload: {
              noticeTableColumns: response.data.columns || [],
              noticeTableData: response.data.dataSource || [],
              noticeTotal: response.data.pageInfo.total || 0,
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              noticeTableColumns: [],
              noticeTableData: [],
              noticeTotal: 0,
            },
          });
          message.error(response.msg || '查询公告列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            noticeTableColumns: [],
            noticeTableData: [],
            noticeTotal: 0,
          },
        });
        message.error('查询公告列表失败!');
      }
    },
    //查询新闻列表
    *comNewsList({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comNewsList, payload);
      if (response) {
        if (response.code == 0) {
          response.data.dataSource.map((item, index) => {
            item.key = index;
          });
          yield put({
            type: 'setState',
            payload: {
              newsTableColumns: response.data.columns || [],
              newsTableData: response.data.dataSource || [],
              newsTotal: response.data.pageInfo.total || 0,
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              newsTableColumns: [],
              newsTableData: [],
              newsTotal: 0,
            },
          });
          message.error(response.msg || '查询新闻列表失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            newsTableColumns: [],
            newsTableData: [],
            newsTotal: 0,
          },
        });
        message.error('查询新闻列表失败!');
      }
    },
    //查询新闻详情
    *comNewsDetail({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comNewsDetail, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              newsInfo: response.data || {},
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              newsInfo: {},
            },
          });
          message.error(response.msg || '查询新闻详情失败!');
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            newsInfo: {},
          },
        });
        message.error('查询新闻详情失败!');
      }
    },
    //查询相关持仓
    *comAssociatedPosition({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comAssociatedPosition, payload);
      const { type } = payload;
      if (response) {
        if (response.code == 0) {
          response.data.dataSource.map((item, index) => {
            item.key = index;
          });
          if (type == 'news') {
            yield put({
              type: 'setState',
              payload: {
                newsHoldColumns: response.data.columns || [],
                newsHoldData: response.data.dataSource || [],
              },
            });
          } else {
            yield put({
              type: 'setState',
              payload: {
                noticeHoldColumns: response.data.columns || [],
                noticeHoldData: response.data.dataSource || [],
              },
            });
          }
        } else {
          if (type == 'news') {
            yield put({
              type: 'setState',
              payload: {
                newsHoldColumns: [],
                newsHoldData: [],
              },
            });
          } else {
            yield put({
              type: 'setState',
              payload: {
                noticeHoldColumns: [],
                noticeHoldData: [],
              },
            });
          }
          message.error(response.msg || '查询相关持仓列表失败!');
        }
      } else {
        if (type == 'news') {
          yield put({
            type: 'setState',
            payload: {
              newsHoldColumns: [],
              newsHoldData: [],
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              noticeHoldColumns: [],
              noticeHoldData: [],
            },
          });
        }
        message.error('查询相关持仓列表失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
